const BaseController = require("../core/baseController");

/**
 *  用来展示 手机端 商店信息的controller
 *
 */
class ShopInfoController extends BaseController {
  async query() {
    const { service } = this;
    const ret = await service.shopInfo.query();
    this.success(ret);
  }

  async create() {
    const { ctx, service } = this;
    const {
      request: {
        body: {
          name,
          shopPicture,
          businessTime,
          longitude,
          latitude,
          pictures,
          contactPeople,
          phone,
          address,
          activities
        }
      }
    } = ctx;
    const shopInfo = {
      name,
      shopPicture,
      businessTime,
      longitude,
      latitude,
      pictures,
      contactPeople,
      phone,
      address,
      activities
    };
    try {
      await service.shopInfo.create(shopInfo);
      const ret = await service.shopInfo.query();
      this.success(ret);
    } catch (e) {
      this.failure("新增失败",e);
    }
  }

  async update() {
    const { ctx, service } = this;
    const {
      request: {
        body: {
          _id,
          name,
          shopPicture,
          businessTime,
          longitude,
          latitude,
          pictures,
          contactPeople,
          phone,
          address
        }
      }
    } = ctx;
    const shopInfo = {
      _id,
      name,
      shopPicture,
      businessTime,
      longitude,
      latitude,
      pictures,
      contactPeople,
      phone,
      address
    };
    try {
      await service.shopInfo.update(shopInfo);
      const ret = await service.shopInfo.query();
      this.success(ret);
    } catch (e) {
      this.failure("更新失败",e);
    }
  }
  async delete() {
    try {
      const ret = this.service.shopInfo.delete();
      this.success(ret);
    } catch (e) {
      this.failure("删除成功", e);
    }
  }

}

module.exports = ShopInfoController;
